package com.yuanye.recruitCrawler.manual;

import java.util.Date;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.yuanye.recruitCrawler.constant.Boss20220630Constant;
import com.yuanye.recruitCrawler.entity.Position;
import com.yuanye.recruitCrawler.utils.ScannerUtil;
import com.yuanye.recruitCrawler.utils.SeleniumUtil;

/** 
 * @author yuanye 2021年7月17日 下午4:27:32
 */
public class BossZhipin{
	private static Logger log=LoggerFactory.getLogger(BossZhipin.class);
	
	public static void main(String[] args) {
		String url="https://www.zhipin.com/web/geek/job?query=Java&city=101010100";
		WebDriver driver = SeleniumUtil.getChromeDriver();
		driver.get(url);
		while(ScannerUtil.scannerReady()) {
			extractPositions(driver);
		}
		if(driver!=null) {
			driver.quit();
		}
	}
	
	public static void extractPositions(WebDriver driver) {
		log.info("extracting URL:{}",driver.getCurrentUrl());
		List<WebElement> content=driver.findElements(By.xpath(Boss20220630Constant.positionList));
		if(content==null||content.size()==0) {
			content=driver.findElements(By.xpath(Boss20220630Constant.positionList_geek));
		}
		if(content==null||content.size()==0) {
			content=driver.findElements(By.className("job-card-wrapper"));
		}
		for(int i=0;i<content.size();i++) {
			WebElement node=content.get(i);
			try {
				String positionName=node.findElement(By.xpath(Boss20220630Constant.positionName)).getText();
				String positionUrl=node.findElement(By.xpath(Boss20220630Constant.positionUrl)).getAttribute("href");
				if(positionUrl!=null&&positionUrl.indexOf(".html")>0) {
					positionUrl=positionUrl.substring(0,positionUrl.indexOf(".html")+5);
				}
				String salary=node.findElement(By.xpath(Boss20220630Constant.salary)).getText();
//				String experience=node.findElement(By.xpath(Boss20220630Constant.experience)).getText();
//				String education=node.findElement(By.xpath(Boss20220630Constant.education)).getText();
				String keyWords=node.findElement(By.xpath(Boss20220630Constant.keyWords)).getText();
				String attraction=node.findElement(By.xpath(Boss20220630Constant.attraction)).getText();
				String companyName=node.findElement(By.xpath(Boss20220630Constant.companyName)).getText();
				String companyUrl=node.findElement(By.xpath(Boss20220630Constant.companyUrl)).getAttribute("href");
				String industry=node.findElement(By.xpath(Boss20220630Constant.industry)).getText();
				String companyLogo=node.findElement(By.xpath(Boss20220630Constant.companyLogo)).getAttribute("src");
				String address=node.findElement(By.xpath(Boss20220630Constant.address)).getText();
				String source="boss";
				
				Position position=new Position();
				position.setPositionName(positionName);
				position.setPositionUrl(positionUrl);
				position.setSalary(salary);
				position.setCompanyName(companyName);
				position.setCompanyUrl(companyUrl);
				position.setAddress(address);
				position.setSource(source);
				position.setCreateTime(new Date());
				//position.setExperience(experience);
				position.setKeyWords(keyWords);
				position.setAttraction(attraction);
				position.setIndustry(industry);
				position.setCompanyLogo(companyLogo);
				System.out.println(JSON.toJSONString(position));
				//saveJD...
			} catch (Exception e) {
				log.error(e.getMessage(),e);
			}
		}
		content=null;
	}
}